package com.android.test.model;

import java.util.ArrayList;

import com.android.test.R;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.android.test.activity.OrgListActivity;

public class OrgListAdapter extends ArrayAdapter<OrgInfo> {
    private static final String TAG = OrgListAdapter.class.getSimpleName();
    private static final int SECOND = 1000;
    private static final int MINUTE = 60 * SECOND;
    private static final int HOUR = 60 * MINUTE;
    private static final int DAY = 24 * HOUR;

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private Drawable thumb = null;

    public static int mOrgPosition;

    public OrgListAdapter(Context context, int textViewResourceId) {
	super(context, textViewResourceId);
	// TODO Auto-generated constructor stub
	mLayoutInflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	mContext = context;

    }

    @Override
    public long getItemId(int position) {
	return getItem(position)._id;
    }

    static class ViewHolder {
	ImageView thumbnail;
	TextView title;
	TextView distance;
	TextView operation;
	RatingBar rating;
	int _id;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	OrgInfo item = getItem(position);
	Log.e("zzy","getView");
	thumb = null;
	ViewHolder holder;
	if (convertView == null
		|| !(convertView.getTag() instanceof ViewHolder)) {
	    convertView = mLayoutInflater.inflate(R.layout.org_list_snippet,
		    parent, false);
	    holder = new ViewHolder();
	    holder.thumbnail = (ImageView) convertView
		    .findViewById(R.id.thumbnail);
	    holder.title = (TextView) convertView.findViewById(R.id.title);
	    holder.distance = (TextView) convertView
		    .findViewById(R.id.distance);
	    holder.operation = (TextView) convertView
		    .findViewById(R.id.operation);
	    holder.rating = (RatingBar) convertView.findViewById(R.id.rating);
	    convertView.setTag(holder);
	} else {
	    holder = (ViewHolder) convertView.getTag();
	}

	// 设置ListView背景颜色
	if (position % 2 != 0) {
	    convertView.setBackgroundResource(R.drawable.list_selector_grey);
	} else {
	    convertView.setBackgroundResource(R.drawable.list_selector_white);
	}

	if (mContext instanceof OrgListActivity) {
	    thumb = ((OrgListActivity) mContext).getThumbnail(position,
		    item._id);
	}

	holder.rating.setVisibility(View.GONE);

	setViewImage(holder.thumbnail, thumb);

	holder.title.setText(item.shortName);
	holder.distance.setText(item.distance);
	holder.rating.setRating(item.rating);

	holder.operation.setVisibility(View.GONE);

	TextView btn = null;

	btn = holder.operation;

	btn.setVisibility(View.VISIBLE);
	setViewButton(btn, R.drawable.list_button, 0);

	return convertView;
    }

    private void setViewImage(ImageView imageView, Object obj) {
	Drawable drawable = imageView.getDrawable();
	if (drawable != null) {
	    drawable.setCallback(null);
	}
	if (obj instanceof Drawable) {
	    imageView.setImageDrawable((Drawable) obj);
	    return;
	}
    }

    private void setViewButton(TextView button, int btnBgResId, int textResId) {
	if (btnBgResId != 0) {
	    button.setBackgroundResource(btnBgResId);
	}

	if (textResId != 0) {
	    button.setText(textResId);
	}
    }

    private View.OnClickListener listener = new View.OnClickListener() {
	public void onClick(View v) {
	}
    };
}
